package com.hama.zea.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Random;

/**
 * 根据word计算random的seed
 * Created by Administrator on 2016/10/14.
 */
public class GetSeed {
    public static final int min=-2147483648;
    public static final int max=2147483647;
    public static void main(String[] args) {
        print("seed:"+getSeedByWord("wings",min));
    }
    public static int getSeedByWord(String word,int init){
        Long start=System.currentTimeMillis();
        Random random=new Random(init);
        while(true){
            if(init==max){
                break;
            }
            init++;
            String str=returnWord(random,init);
            if(str.equals(word)){
                break;
            }
        }
        print("it spents:"+(System.currentTimeMillis()-start+"ms"));
        return  init;
    }

    public static String returnWord(Random random, int seed){
        random.setSeed(seed);
        StringBuilder sb = new StringBuilder();
        while (true){
            int k=random.nextInt(27);
            //System.out.println("char:" + k + ",number:" +  k);
            if(k==0)
                break;
            k+=96;
            sb.append((char)k);
        }
        return sb.toString();
    }


    public static void print(Object str){
        System.out.println(str);
    }

}
